const { ccclass, property } = cc._decorator;

@ccclass
export default class TransactionTs extends cc.Component {

    @property(cc.Label)
    labelTitle1: cc.Label = null;

    @property(cc.Label)
    labelTitle2: cc.Label = null;

    @property(cc.Label)
    labelTitle3: cc.Label = null;

    @property(cc.Label)
    labelTitle4: cc.Label = null;

    @property(cc.Label)
    labelTitle5: cc.Label = null;

    @property(cc.Label)
    labelNoRecord: cc.Label = null;

    @property(cc.Prefab)
    listItem: cc.Prefab = null;

    public itemDate: Array<any> = [];

    public imgBtnBg1: cc.Sprite = null;
    public imgBtnBg2: cc.Sprite = null;
    public imgBtnBg3: cc.Sprite = null;
    public imgBtnBg4: cc.Sprite = null;
    public imgBtnBg5: cc.Sprite = null;

    public labelBtnBg1: cc.Label = null;
    public labelBtnBg2: cc.Label = null;
    public labelBtnBg3: cc.Label = null;
    public labelBtnBg4: cc.Label = null;
    public labelBtnBg5: cc.Label = null;

    public clickBtnName: string = "";

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.imgBtnBg1 = this.node.getChildByName("groupAll").getChildByName("buttonAll").getChildByName("imgbtnBg1").getComponent(cc.Sprite);
        this.imgBtnBg2 = this.node.getChildByName("groupAll").getChildByName("buttonWon").getChildByName("imgbtnBg2").getComponent(cc.Sprite);
        this.imgBtnBg3 = this.node.getChildByName("groupAll").getChildByName("buttonLost").getChildByName("imgbtnBg3").getComponent(cc.Sprite);
        this.imgBtnBg4 = this.node.getChildByName("groupAll").getChildByName("buttonAdded").getChildByName("imgbtnBg4").getComponent(cc.Sprite);
        this.imgBtnBg5 = this.node.getChildByName("groupAll").getChildByName("buttonWithdrawal").getChildByName("imgbtnBg5").getComponent(cc.Sprite);

        this.labelBtnBg1 = this.node.getChildByName("groupAll").getChildByName("buttonAll").getChildByName("label").getComponent(cc.Label);
        this.labelBtnBg2 = this.node.getChildByName("groupAll").getChildByName("buttonWon").getChildByName("label").getComponent(cc.Label);
        this.labelBtnBg3 = this.node.getChildByName("groupAll").getChildByName("buttonLost").getChildByName("label").getComponent(cc.Label);
        this.labelBtnBg4 = this.node.getChildByName("groupAll").getChildByName("buttonAdded").getChildByName("label").getComponent(cc.Label);
        this.labelBtnBg5 = this.node.getChildByName("groupAll").getChildByName("buttonWithdrawal").getChildByName("label").getComponent(cc.Label);
        this.onImgBtnBgIsShow(0);
    }

    public initTransactionTs(resultData) {
        if(!this.labelNoRecord) return

        this.itemDate = resultData;
        this.itemDate.sort((a, b) => {
            return b.createdAt - a.createdAt;
        })
        cc.log(this.itemDate);
        if (this.itemDate.length > 0) {
            this.labelNoRecord.node.active = false;
        } else {
            this.labelNoRecord.node.active = true;
        }
        let content = this.node.getChildByName("groupAll").getChildByName("ScrollView").getChildByName("view").getChildByName("content");
        content.removeAllChildren();
        let aaa = this.itemDate;
        for (const k of this.itemDate) {
            let itemNode = cc.instantiate(this.listItem);
            content.addChild(itemNode);
            itemNode.getComponent("transaction_details_itemTs").setInfo(k);
        }
    }

    public onBtnClose() {
        // cc.gg.gameDataUtil.goToLobby(true);
        this.node.destroy();
    }

    public onBtnClick(event) {
        this.itemDate = [];
        let btnName = event.target.name;
        if (this.clickBtnName == btnName) return;
        if (btnName == "buttonAll") {
            this.getInvoiceList(0);
            this.onImgBtnBgIsShow(0);
        } else if (btnName == "buttonWon") {
            this.getInvoiceList(1);
            this.onImgBtnBgIsShow(1);
        } else if (btnName == "buttonLost") {
            this.getInvoiceList(2);
            this.onImgBtnBgIsShow(2);
        } else if (btnName == "buttonAdded") {
            this.getInvoiceList(3);
            this.onImgBtnBgIsShow(3);
        } else if (btnName == "buttonWithdrawal") {
            this.getInvoiceList(4);
            this.onImgBtnBgIsShow(4);
        }
        this.clickBtnName = btnName;
    }

    public onImgBtnBgIsShow(index: number) {
        this.imgBtnBg1.node.active = false;
        this.imgBtnBg2.node.active = false;
        this.imgBtnBg3.node.active = false;
        this.imgBtnBg4.node.active = false;
        this.imgBtnBg5.node.active = false;

        this.labelBtnBg1.node.color = cc.Color.BLACK;
        this.labelBtnBg2.node.color = cc.Color.BLACK;
        this.labelBtnBg3.node.color = cc.Color.BLACK;
        this.labelBtnBg4.node.color = cc.Color.BLACK;
        this.labelBtnBg5.node.color = cc.Color.BLACK;

        this[`imgBtnBg${index + 1}`].node.active = true;
        this[`labelBtnBg${index + 1}`].node.color = cc.Color.WHITE;
    }

    /**最近交易信息
     * 0-全部   1-胜利   2-失败    3-充值或奖励   4-体现
     */
    public getInvoiceList(Index: number) {
        cc.gg.httpUtil.httpGet('/invoice/list/get', { filter: Index }, function (result) {
            if (!result.succ) {
                this.itemDate = null;
            } else {
                this.initTransactionTs(result.data);
            }
        }.bind(this));
    }

    // update (dt) {}
}
